var SysUser = {

    table: $('#tb'),

    columns: [{
        checkbox: true    // 是否显示复选框
    }, {
        field: 'id',
        title: 'ID',
    }, {
        field: 'userNick',
        title: '用户名'
    }, {
        field: 'userName',
        title: '账户名'
    }, {
        field: 'userEmail',
        title: '用户邮箱'
    }, {
        field: 'userMobile',
        title: '手机号'
    }, {
        field: 'userSex',
        title: '性别',
        formatter: function (value) {
            if (value == "1") {
                return "男";
            }
            return "女";
        }
    }, {
        field: 'loginStatus',
        title: '状态',
        formatter: function (value) {
            if (value == "1") {
                return "<span class='label label-success'>启用</span>";
            }
            return "<span class='label label-danger'>禁用</span>";
        }
    }],

    refresh: function () {
        SysUser.table.bootstrapTable('refresh', {
            query: {
                userName: $("#userName").val(),
                userMobile: $("#userMobile").val()
            }
        });
    }
};

$(function () {
    SysUser.table.bootstrapTable({
        classes: 'table table-bordered table-hover table-striped',
        url: ctx + '/sys/user/list',
        method: 'get',
        uniqueId: 'id',
        idField: 'id',
        toolbar: '#toolbar',
        clickToSelect: true,
        showColumns: true,
        showRefresh: true,
        pagination: true,
        sidePagination: "server",
        pageNumber: 1,
        pageSize: 10,
        pageList: [10, 25, 50, 100],
        columns: SysUser.columns
    });

    /**
     * 添加
     */
    $("#add").click(function () {
        open(ctx + "/sys/user/add", "添加用户", function () {
            SysUser.refresh();
        }, true);
    });

    /**
     * 更新
     */
    $("#edit").click(function () {
        if (check(SysUser.table)) {
            var row = getRow(SysUser.table);
            open(ctx + "/sys/user/edit?id=" + row.id, "修改用户", function () {
                SysUser.refresh();
            }, true);
        }

    });

    /**
     * 删除
     */
    $("#delete").click(function () {
        if (check(SysUser.table)) {
            var row = getRow(SysUser.table);
            confirm("确定删除该用户吗?", function () {
                var ax = new $ax(ctx + "/sys/user/delete?id=" + row.id, function (result) {
                    if (result.success) {
                        success("删除成功");
                        SysUser.refresh();
                    }
                });
                ax.start();
            });
        }
    });

    /**
     * 启用
     */
    $("#enable").click(function () {
        if (check(SysUser.table)) {
            var row = getRow(SysUser.table);
            confirm("确定启用该用户吗?", function () {
                var ax = new $ax(ctx + "/sys/user/update", function (result) {
                    if (result.success) {
                        success("启用成功!");
                        SysUser.refresh();
                    }
                });
                ax.set("id", row.id);
                ax.set("loginStatus", "1");
                ax.start();
            });
        }
    });

    /**
     * 禁用
     */
    $("#unable").click(function () {
        if (check(SysUser.table)) {
            var row = getRow(SysUser.table);
            confirm("确定禁用该用户吗?", function () {
                var ax = new $ax(ctx + "/sys/user/update", function (result) {
                    if (result.success) {
                        success("禁用成功");
                        SysUser.refresh();
                    }
                });
                ax.set("id", row.id);
                ax.set("loginStatus", "2");
                ax.start();
            });
        }
    });

    /**
     * 刷新
     */
    $("#refresh").click(function () {
        SysUser.refresh();
    });

    var setting = {
        check: {
            enable: true,
            chkStyle: "checkbox",    //复选框
            chkboxType: {
                "Y": "ps",
                "N": "ps"
            }
        },
        data: {
            simpleData: {
                enable: true,//是否采用简单数据模式
                idKey: "id",//树节点ID名称
                pIdKey: "pId",//父节点ID名称
                rootPId: null,//根节点ID
            }
        }
    };

    /**
     * 分配权限
     */
    $("#role").click(function () {
        if (check(SysUser.table)) {
            var row = getRow(SysUser.table);
            var ax = new $ax(ctx + "/sys/role/queryRolesZtree?userId=" + row.id, function (result) {
                if (result.success) {
                    var index = layer.open({
                        type: 1,
                        btn: ["确定", "取消"],
                        yes: function () {
                            var checkedNodes = treeObj("tree").getCheckedNodes();
                            var ids = "";
                            for (var i = 0; i < checkedNodes.length; i++) {
                                ids = ids + checkedNodes[i].id + ",";
                            }
                            var ax2 = new $ax(ctx + "/sys/role/addUserRole", function (result) {
                                if (result.success) {
                                    success("分配成功");
                                    layer.close(index);
                                }
                            });
                            ax2.set("userId", row.id);
                            ax2.set("roleIds", ids);
                            ax2.start();

                        },
                        title: "分配角色",
                        area: ['300px', '350px'],
                        content: "<div><ul id='tree' class='ztree'></ul></div>",
                    });
                    $.fn.zTree.init($("#tree"), setting, result.data);
                }
            });
            ax.start();
        }
    });
})
